package com.fingard.dsp.bank.directbank.cmb01;

import java.io.File;
import java.util.*;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillQuery;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.*;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB001OperBillQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB005BillBackQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB006BillStateQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB007BillKzsrQryDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
* @Author Green
* */
@SuppressWarnings("ALL")
public class CMB01ElecBill  extends CMB01Base implements IElecBillQuery{
    @Override
    /**
    * 可操作票据查询
     * 业务类型 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回                 12-提示付款 13-追索 14-同意清偿
     * 查询类型 1-申请 2-应答 3-撤回
    * */
    public RetAtsB001OperBillQry D_OperBill_Query_Req(ReqB001OperBillQry query) throws Exception {
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);

        retAts.writeStart();

        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CMB01Header header = new CMB01Header();
            //region 待处理票据查询
            int busiType = Integer.valueOf(query.detail.busiType);
            String buscode;
            //查询非同意清偿的可操作票据
            if (("1".equals(query.detail.queryType)&&!"14".equals(query.detail.busiType))||
                    ("3".equals(query.detail.queryType)&&"1".equals(query.detail.busiType))) {
                retAts.writeRespCode("0");
                retAts.writeRespInfo("");
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                String start = getBankConfig().getValueAsString("DateBeforeNow");
                int monthStart = StringHelper.hasAnyChar(start)?Integer.valueOf(start):0;
                String queryLength = getBankConfig().getValueAsString("QueryLength");
                int dateLength = StringHelper.hasAnyChar(queryLength)?Integer.valueOf(queryLength):12;
                //获取查询时间的起点
                calendar.set(Calendar.DAY_OF_MONTH,1);
                calendar.add(Calendar.MONTH,monthStart);
                String calendarStart =Format.Date8Format.format(calendar.getTime());
                //获取查询时间的终点
                calendar.add(Calendar.MONTH,dateLength-1);
                calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                String calendarEnd =Format.Date8Format.format(calendar.getTime());

                //获取查询时间的临时点
                String calendarMid;
                if(dateLength>3) {
                    calendar.add(Calendar.MONTH,2-(dateLength-1));
                    calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                    calendarMid =Format.Date8Format.format(calendar.getTime());
                }else{
                    calendarMid = calendarEnd;
                }
                do {
                    xmlWriter = new XmlTextWriter();
                    header.FUNNAM = "SDKNTINQOPR";
                    writeCommonHeader(xmlWriter, header);
                    xmlWriter.writeStartElement("NTINQOPRY");
//                BBKNBR 分行号 C(2) 否
                    String bankarea = getBankConfig().getMapValueByKey("bankArea", query.detail.actArea);
                    if ("308584001024".equals(query.detail.actArea)) {
                        bankarea = "1";
                    }
                    xmlWriter.writeElementString("BBKNBR", 4 == query.detail.actArea.length() ? bankarea : query.detail.actArea);
//                ACCNBR 帐号 C(35) 否
                    xmlWriter.writeElementString("ACCNBR", query.detail.actNum);
                    /// 业务类型 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
                    switch (busiType) {
                        case 1:
                            if ("1".equals(query.detail.queryType)) {
                                buscode = "01";
                            } else {
                                buscode = "04";//撤票
                            }
                            break;//出票
                        case 2:
                            buscode = "02";
                            break;//承兑
                        case 3:
                            buscode = "03";
                            break;//收票
                        case 4:
                            buscode = "10";
                            break;//背书
                        case 5:
                            buscode = "18";
                            break;//质押
                        case 6:
                            buscode = "19";
                            break;//质押解除
                        case 10:
                            buscode = "11";
                            break;//贴现
                        case 12:
                            if ("1".equals(query.detail.overdueFlag)) {
                                buscode = "20";
                            } else {
                                buscode = "21";
                            }
                            break;//提示付款
                        case 13:
                            buscode = "22";
                            break;//追索
                        default:
                            throw new Exception("未知的业务类型：" + query.detail.busiType);
                    }
                    xmlWriter.writeElementString("BUSCOD", buscode);
//                BGUTYP 保证类型 C(2) A.30 保证类型 EDBGUTYP 可 业务编码为 17：保证 时，必输。
                    xmlWriter.writeElementClosed("BGUTYP");
//                BILNBR 人行电子票 据号码 C(30) 可
                    xmlWriter.writeElementClosed("BILNBR");
//                BGNDAT 票据到期日 起始日期 D 否
//                    String beginDate = getBankConfig().getValueAsString("BeginDate");
                    xmlWriter.writeElementString("BGNDAT", calendarStart);
//                ENDDAT 票据到期日 结束日期 D 否
//                    String endDate = getBankConfig().getValueAsString("EndDate");
                    xmlWriter.writeElementString("ENDDAT", calendarMid);
//                BGNAMT 最小票据金 额 M 可
                    xmlWriter.writeElementClosed("BGNAMT");
//                ENDAMT 最大起始金额 M 可;
                    xmlWriter.writeElementClosed("ENDAMT");
                    xmlWriter.writeEndDocument();
                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpbkMsg = new StringBuilder();
                        XmlTextReader tmpbkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                        if (tmpbkReader != null) {
                            String returnCode = tmpbkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                            String returnInfo = tmpbkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                            if ("0".equals(returnCode)) {
                                while (tmpbkReader.read("/CMBSDKPGK/NTINQOPRZ")) {
                                    String billtype = "AC01".equals(tmpbkReader.readedXElement.getSingleText("BILTYP")) ? "1" : "2";
                                    if (query.detail.billType.equals(billtype)) {
                                        RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
//                                BILNBR 人民银行票号 C(30) 可
                                        detail.billNo = tmpbkReader.readedXElement.getSingleText("BILNBR");
//                                BILTYP 票据种类 C(4) A.1 票据种类 可
//                                BILCCY 票据货币 C(2) 可
//                                BILAMT 票据金额 M 可
                                        detail.billAmt = tmpbkReader.readedXElement.getSingleText("BILAMT");
                                        detail.billType = "1".equals(billtype) ? "EM001" : "EM002";
//                                ISUDAT 出票日期 D 可
                                        detail.issDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("ISUDAT"));
//                                DUEDAT 到期日期 D 可
                                        detail.dueDate = DateHelper.convertD8toD10(tmpbkReader.readedXElement.getSingleText("DUEDAT"));
//                                DRWNAM 出票人名称 Z(100) 可
                                        detail.drawName = tmpbkReader.readedXElement.getSingleText("DRWNAM");
//                                DRWEAC 出票人账号 C(35) 可
                                        detail.drawAct = tmpbkReader.readedXElement.getSingleText("DRWEAC");
//                                PYENAM 收票人名称 Z(100) 可
                                        detail.payeeName = tmpbkReader.readedXElement.getSingleText("PYENAM");
//                                PYEEAC 收票人账号 C(35) 可
                                        detail.payeeAct= tmpbkReader.readedXElement.getSingleText("PYEEAC");
//                                ACPNAM 承兑人名称 Z(100) 可
                                        detail.accpName = tmpbkReader.readedXElement.getSingleText("ACPNAM");
//                                ACPBRD 承兑人行号 C(12) 可
                                        detail.accpBnkCode = tmpbkReader.readedXElement.getSingleText("ACPBRD");
//                                ACPEAC 承兑人账号 C(35) 可
                                        detail.accpAct = tmpbkReader.readedXElement.getSingleText("ACPEAC");
//                                PBLSTS 人行票据状态 C(6) A.31 参数 EDPBLSTS 可
                                        detail.billStatus = tmpbkReader.readedXElement.getSingleText("PBLSTS");
                                        if ("20".equals(buscode)) {
                                            detail.overDueFlag = "1";
                                        } else if ("21".equals(buscode)) {
                                            detail.overDueFlag = "2";
                                        }
                                        retAts.writeDetail(detail);
                                    }
                                }
                                calendar.add(Calendar.DAY_OF_MONTH,1);
                                calendarStart = Format.Date8Format.format(calendar.getTime());
                                calendar.add(Calendar.MONTH,3);
                                calendar.add(Calendar.DAY_OF_MONTH,-1);
                                calendarMid = Format.Date8Format.format(calendar.getTime());
                                if(calendarMid.compareTo(calendarEnd)>0){
                                    calendarMid = calendarEnd;
                                }
                            } else {
                                throw new Exception(returnInfo);
                            }
                        } else {
                            throw new Exception(tmpbkMsg.toString());
                        }
                    } else {
                        throw new Exception(tmpStrRet[1]);
                    }
                }while (calendarStart.compareTo(calendarEnd)<0);
            }else if("2".equals(query.detail.queryType)&&"12".equals(query.detail.busiType)){
                for(int overdueFlagInt =1;overdueFlagInt<3;overdueFlagInt++){
                    header.FUNNAM = "SDKNTWATANS";
                    xmlWriter = new XmlTextWriter();
                    writeCommonHeader(xmlWriter, header);

                    xmlWriter.writeStartElement("NTWATANSY");
                    //帐号
                    xmlWriter.writeElementString("ACCNBR", query.detail.actNum);
                    //业务编码 空为查询所有
                    if(overdueFlagInt==1) {
                        xmlWriter.writeElementString("BUSCOD", "20");
                    }else{
                        xmlWriter.writeElementString("BUSCOD", "21");
                    }
                    //人行电子票据号码
                    xmlWriter.writeElementString("BILNBR", "");
                    //服务状态
                    //A1：提回待处理
                    //20：可撤销
                    xmlWriter.writeElementString("CLTSTS", "A1");
                    xmlWriter.writeEndDocument();
                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                    queryBill(""+overdueFlagInt, tmpStrRet, retAts,query.detail.billType);
                }
            }
            //查询签收类操作和同意清偿
            else if ("2".equals(query.detail.queryType)||("1".equals(query.detail.queryType)&&"14".equals(query.detail.busiType))) {
                header.FUNNAM = "SDKNTWATANS";
                xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, header);

                xmlWriter.writeStartElement("NTWATANSY");
                //帐号
                xmlWriter.writeElementString("ACCNBR", query.detail.actNum);
                switch (busiType) {
                    case 2:
                        buscode = "02";
                        break;//承兑签收
                    case 3:
                        buscode = "03";
                        break;//提示收票签收
                    case 4:
                        buscode = "10";
                        break;//背书签收
                    case 5:
                        buscode = "18";
                        break;//质押签收
                    case 6:
                        buscode = "19";
                        break;//质押解除签收
                    case 11:
                        buscode = "12";
                        break;//贴现赎回签收
                    case 14:
                        if("1".equals(query.detail.queryType)){
                            buscode = "22";
                        }else{
                            buscode = "23";
                        }
                        break;//同意清偿签收
                    default:
                        throw new Exception("未知的业务类型：" + query.detail.busiType);
                }
                //业务编码 空为查询所有
                xmlWriter.writeElementString("BUSCOD", buscode);
                //人行电子票据号码
                xmlWriter.writeElementString("BILNBR", "");
                //服务状态
                //A1：提回待处理
                //20：可撤销
                xmlWriter.writeElementString("CLTSTS", "A1");
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                queryBill("", tmpStrRet, retAts,query.detail.billType);
            } else if ("3".equals(query.detail.queryType)) {
                //region 可撤销票据查询
                header.FUNNAM = "SDKNTINQCNL";
                xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, header);

                xmlWriter.writeStartElement("NTWATANSY");
                //帐号
                xmlWriter.writeElementString("ACCNBR", query.detail.actNum);
                switch (busiType) {
                    case 2:
                        buscode = "02";
                        break;//承兑撤回
                    case 3:
                        buscode = "03";
                        break;//提示收票撤回
                    case 4:
                        buscode = "10";
                        break;//背书撤回
                    case 5:
                        buscode = "18";
                        break;//质押撤回
                    case 6:
                        buscode = "19";
                        break;//质押解除撤回
                    case 10:
                        buscode = "11";
                        break;//贴现撤回
                    case 12:
                        if ("1".equals(query.detail.overdueFlag)) {
                            buscode = "20";
                        } else {
                            buscode = "21";
                        }
                        break;//提示付款撤回
                    case 13:
                        buscode = "22";
                        break;//追索撤回
                    case 14:
                        buscode = "23";
                        break;//清偿撤回
                    default:
                        throw new Exception("未知的业务类型：" + query.detail.busiType);
                }
                //业务编码 空为查询所有
                xmlWriter.writeElementString("BUSCOD", buscode);
                //人行电子票据号码
                xmlWriter.writeElementString("BILNBR", "");
                //服务状态
                //A1：提回待处理
                //20：可撤销
                xmlWriter.writeElementString("CLTSTS", "20");
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                queryBill("", tmpStrRet, retAts,query.detail.billType);
                //endregion
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();

        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
    * 操作明细查询
    * */
    public RetAtsB002BillTransQry D_B002_BillTrans_Query(ReqB002BillTransQry query) throws Exception {
        return null;
    }

    @SuppressWarnings("MapOrSetKeyShouldOverrideHashCodeEquals")
    @Override
    /**
     * 正面信息查询
     */
    public RetAtsB004BillFrontQry D_B004_BillFront_Query(ReqB004BillFrontQry query) throws Exception {
        RetAtsB004BillFrontQry retAts = new RetAtsB004BillFrontQry(query.reqHeader);
        retAts.writeStart();
        query.readRecord();
        //待签收票据先用10接口查询
        if("2".equals(query.detail.queryType)||
                (("1".equals(query.detail.queryType)&&"14".equals(query.detail.busiType)))){
            queryBillInfoBySign(retAts,query);
        }
        if(StringHelper.isNullOrEmpty(retAts.billNo))
        {
            queryBillInfoByOwn(retAts, query.detail.actNum, query.detail.billNo);
        }

        retAts.writeRetDetail();
        retAts.writeEnd();
        return retAts;
    }
    //待签收者查询票据的正面信息
    private void queryBillInfoBySign(RetAtsB004BillFrontQry retAts, ReqB004BillFrontQry query) {
        try {
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "SDKNTWATAEX";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header);
//            NTINQDRFY
            xmlWriter.writeStartElement("NTWATANSY");
//           ACCNBR 帐号 C(35) 否
            xmlWriter.writeElementString("ACCNBR",query.detail.actNum);
//            BUSCOD 业务编码 C(50) 企业：A.10 参数
            String buscode;
//            03=提示收票回复
//            22=追索回复
//            23=同意清偿回复
//            18=质押申请回复
//            19=质押解除回复
//            12=贴现赎回回复
//            10=背书转让回复
            if("2".equals(query.detail.busiType)){
                buscode = "02";
            }else if("3".equals(query.detail.busiType)){
                buscode= "03";
            }else if("4".equals(query.detail.busiType)){
                buscode= "10";
            }else if("5".equals(query.detail.busiType)){
                buscode= "18";
            }else if("6".equals(query.detail.busiType)){
                buscode= "19";
            }else if("11".equals(query.detail.busiType)){
                buscode= "12";
            }else if("12".equals(query.detail.busiType)){
                if("1".equals(query.detail.overdueFlag)){
                    buscode= "20";
                }else{
                    buscode= "21";
                }
            }else if("14".equals(query.detail.busiType)){
                if("1".equals(query.detail.queryType)) {
                    buscode = "22";
                }else{
                    buscode = "23";
                }
            }else{
                throw new Exception("未知的操作类型！"+query.detail.busiType);
            }
            xmlWriter.writeElementString("BUSCOD",buscode);
//            BILNBR 人 行 电 子 票 据号码 C(30) 可
            xmlWriter.writeElementString("BILNBR",query.detail.billNo);
//            CLTSTS 服务状态 C(2) A1：提回待处理 20：可撤销 否
            xmlWriter.writeElementString("CLTSTS","A1");
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpSbReader = loadBankRespXmlReader(tmpStrRet[1],tmpSbMsg);
                if(tmpSbReader!=null){
                    String returnCode = tmpSbReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    String retrunInfo = tmpSbReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    if("0".equals(returnCode)){
                        retAts.respCode = returnCode;
                        retAts.respInfo = retrunInfo;
                        if(tmpSbReader.read("/CMBSDKPGK/NTDRFDTLZ1")){
////                          BILNBR 人行电子票据号码C(30) 可
                            retAts.billNo = tmpSbReader.readedXElement.getSingleText("BILNBR");
//                            BILTYP 票据种类 C(4) A.1 票据种类 可
                            retAts.billType = "AC01".equals(tmpSbReader.readedXElement.getSingleText("BILTYP"))?
                                    "1":"2";
//                            BILAMT 票据金额 M 可
                            retAts.billAmt = tmpSbReader.readedXElement.getSingleText("BILAMT");
//                            ISUDAT 出票日期 D 可
                            retAts.issDate = DateHelper.convertD8toD10(tmpSbReader.readedXElement.getSingleText("ISUDAT"));
//                            DUEDAT 到期日 D 可
                            retAts.dueDate= DateHelper.convertD8toD10(tmpSbReader.readedXElement.getSingleText("DUEDAT"));
//                            NOTTRF 不得转让标记 C(4) A.2 不得转让标记
                            String trf = tmpSbReader.readedXElement.getSingleText("ENDTRF");
                            if(!StringHelper.hasAnyChar(trf)){
                                trf =tmpSbReader.readedXElement.getSingleText("NOTTRF ");
                            }
                            retAts.transFlag = "EM01".equals(tmpSbReader.readedXElement.getSingleText("ENDTRF"))?
                                    "0":"1";
//                            PAYPMT 到期无条件支 付承诺 C(4)
                            retAts.mustPay = "CC01".equals(tmpSbReader.readedXElement.getSingleText("PAYPMT"))?
                                    "0":"1";
//                            DRWNAM 出票人名称 Z(200) 否
                            retAts.drawName = tmpSbReader.readedXElement.getSingleText("DRWNAM");
//                            DRWBRD 出票人行号 C(12) 可
                            retAts.drawBnkCode = tmpSbReader.readedXElement.getSingleText("DRWBRD");
//                            DRWBAM 出票人开户银 行 Z(200) 可
                            retAts.drawBnkName = tmpSbReader.readedXElement.getSingleText("DRWBAM");
//                            DRWEAC 出票人账号 C(35) 可
                            retAts.drawAct = tmpSbReader.readedXElement.getSingleText("DRWEAC");
//                            PYENAM 收票人名称 Z(200) 可
                            retAts.payeeName = tmpSbReader.readedXElement.getSingleText("PYENAM");
//                            PYEBRD 收票人行号 C(12) 可
                            retAts.payeeBnkCode = tmpSbReader.readedXElement.getSingleText("PYEBRD");
//                            PYEBAM 收票人开户银行 Z(200) 可
                            retAts.payeeBnkName = tmpSbReader.readedXElement.getSingleText("PYEBAM");
//                            PYEEAC 收票人账号 C(35) 可
                            retAts.payeeAct = tmpSbReader.readedXElement.getSingleText("PYEEAC");
//
//                            ACPNAM 承兑人名称 Z(200) 可
                            retAts.accpName = tmpSbReader.readedXElement.getSingleText("ACPNAM");
//                            ACPBRD 承兑人行号 C(12) 可
                            retAts.accpBnkCode = tmpSbReader.readedXElement.getSingleText("ACPBRD");
//                            ACPBAM 承兑人开户银
                            retAts.accpBnkName = tmpSbReader.readedXElement.getSingleText("ACPBAM");
//                            ACPEAC 承兑人账号 C(35) 可
                            retAts.accpAct = tmpSbReader.readedXElement.getSingleText("ACPEAC");
//                            PBLSTS 人行票据状态 C(6)
                            retAts.billState = tmpSbReader.readedXElement.getSingleText("PBLSTS");
                        }
                    }else{
                        retAts.respCode ="-1";
                        retAts.respInfo = retrunInfo;
                    }
                }else {
                    retAts.respCode ="-1";
                    retAts.respInfo =tmpSbMsg.toString();
                }
            } else {
                retAts.respCode ="-1";
                retAts.respInfo = tmpStrRet[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
        }
    }

    //持票人查询票据的正面信息
    private void queryBillInfoByOwn(RetAtsB004BillFrontQry retAts,String actNum,String billNo){
        try {
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "SDKNTECDBCH";
            XmlTextWriter xmlWriter = new XmlTextWriter();

            writeCommonHeader(xmlWriter, header);
//            NTINQDRFY
            xmlWriter.writeStartElement("NTDRFDTLY");
//            BILNBR 人行电子票据号码 C(30) 可
            xmlWriter.writeElementString("BILNBR",billNo,true);
//            OWNEAC 权利人帐号 C(35) 否 权利人帐号调用查询 票据业务经办业务控制信息
            xmlWriter.writeElementString("OWNEAC",actNum);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpSbReader = loadBankRespXmlReader(tmpStrRet[1],tmpSbMsg);
                if(tmpSbReader!=null){
                    String returnCode = tmpSbReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    String retrunInfo = tmpSbReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    if("0".equals(returnCode)){
                        retAts.respCode = returnCode;
                        retAts.respInfo = retrunInfo;
                        if(tmpSbReader.read("/CMBSDKPGK/NTDRFDTLZ1")){
////                          BILNBR 人行电子票据号码C(30) 可
                            retAts.billNo = tmpSbReader.readedXElement.getSingleText("BILNBR");
//                            BILTYP 票据种类 C(4) A.1 票据种类 可
                            retAts.billType = "AC01".equals(tmpSbReader.readedXElement.getSingleText("BILTYP"))?
                                    "1":"2";
//                            BILAMT 票据金额 M 可
                            retAts.billAmt = tmpSbReader.readedXElement.getSingleText("BILAMT");
//                            ISUDAT 出票日期 D 可
                            retAts.issDate = DateHelper.convertD8toD10(tmpSbReader.readedXElement.getSingleText("ISUDAT"));
//                            DUEDAT 到期日 D 可
                            retAts.dueDate= DateHelper.convertD8toD10(tmpSbReader.readedXElement.getSingleText("DUEDAT"));
//                            NOTTRF 不得转让标记 C(4) A.2 不得转让标记
                            String trf = tmpSbReader.readedXElement.getSingleText("ENDTRF");
                            if(!StringHelper.hasAnyChar(trf)){
                                trf =tmpSbReader.readedXElement.getSingleText("NOTTRF ");
                            }
                            retAts.transFlag = "EM01".equals(tmpSbReader.readedXElement.getSingleText("ENDTRF"))?
                                    "0":"1";
//                            PAYPMT 到期无条件支 付承诺 C(4)
                            retAts.mustPay = "CC01".equals(tmpSbReader.readedXElement.getSingleText("PAYPMT"))?
                                    "0":"1";
//                            DRWNAM 出票人名称 Z(200) 否
                            retAts.drawName = tmpSbReader.readedXElement.getSingleText("DRWNAM");
//                            DRWBRD 出票人行号 C(12) 可
                            retAts.drawBnkCode = tmpSbReader.readedXElement.getSingleText("DRWBRD");
//                            DRWBAM 出票人开户银 行 Z(200) 可
                            retAts.drawBnkName = tmpSbReader.readedXElement.getSingleText("DRWBAM");
//                            DRWEAC 出票人账号 C(35) 可
                            retAts.drawAct = tmpSbReader.readedXElement.getSingleText("DRWEAC");


//                            PYENAM 收票人名称 Z(200) 可
                            retAts.payeeName = tmpSbReader.readedXElement.getSingleText("PYENAM");
//                            PYEBRD 收票人行号 C(12) 可
                            retAts.payeeBnkCode = tmpSbReader.readedXElement.getSingleText("PYEBRD");
//                            PYEBAM 收票人开户银行 Z(200) 可
                            retAts.payeeBnkName = tmpSbReader.readedXElement.getSingleText("PYEBAM");
//                            PYEEAC 收票人账号 C(35) 可
                            retAts.payeeAct = tmpSbReader.readedXElement.getSingleText("PYEEAC");
//
//                            ACPNAM 承兑人名称 Z(200) 可
                            retAts.accpName = tmpSbReader.readedXElement.getSingleText("ACPNAM");
//                            ACPBRD 承兑人行号 C(12) 可
                            retAts.accpBnkCode = tmpSbReader.readedXElement.getSingleText("ACPBRD");
//                            ACPBAM 承兑人开户银
                            retAts.accpBnkName = tmpSbReader.readedXElement.getSingleText("ACPBAM");
//                            ACPEAC 承兑人账号 C(35) 可
                            retAts.accpAct = tmpSbReader.readedXElement.getSingleText("ACPEAC");
//                            PBLSTS 人行票据状态 C(6)
                            retAts.billState = tmpSbReader.readedXElement.getSingleText("PBLSTS");
                        }
                    }else{
                        retAts.respCode ="-1";
                        retAts.respInfo = retrunInfo;
                    }
                }else {
                    retAts.respCode ="-1";
                    retAts.respInfo =tmpSbMsg.toString();
                }
            } else {
                retAts.respCode ="-1";
                retAts.respInfo = tmpStrRet[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
        }
    }

    @Override
    /**
     * 票据背面信息查询
     */
    public RetAtsB005BillBackQry D_B005_BillBack_Query(ReqB005BillBackQry query) throws Exception {
        RetAtsB005BillBackQry retBill = new RetAtsB005BillBackQry(query.reqHeader);
        RetAtsB005BillBackQryDetail detail = new RetAtsB005BillBackQryDetail();
        try {
            XmlTextReader reader = new XmlTextReader(new StringBuilder());
            retBill.writeStart();
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "SDKNTECDBCH";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            query.readRecord();
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("NTDRFDTLY");
            xmlWriter.writeElementString("OWNEAC", query.detail.actNum);
            xmlWriter.writeElementString("BILNBR", query.detail.billNo);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            List<RetAtsB005BillBackQryDetail> arrayList = queryBillInfo(tmpStrRet);
            for (RetAtsB005BillBackQryDetail mydetail : arrayList) {
//                detail = mydetail;
                retBill.writeDetail(mydetail);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retBill = new RetAtsB005BillBackQry(query.reqHeader);
            retBill.writeStart();
            detail.respCode  ="-1";
            detail.respInfo = ex.getMessage();
            detail.billNo = query.detail.billNo;
            retBill.writeDetail(detail);
        }
        retBill.writeEnd();
        return retBill;
    }

    @Override
    /**
     * 票据交易状态查询
     */
    public RetAtsB006BillStateQry D_B006_BillState_Qeury(ReqB006BillStateQry query) throws Exception {
        RetAtsB006BillStateQry retAts = new RetAtsB006BillStateQry(query.reqHeader);
        RetAtsB006BillStateQryDetail detail = new RetAtsB006BillStateQryDetail();
        RetAtsB001OperBillQry tmpRet =  new RetAtsB001OperBillQry(query.reqHeader);
        retAts.writeStart();
        String retId;
        Map retMap  = new HashMap<String,String>(16);
        query.readRecord();
        CMB01Header header  = new CMB01Header();
        //region 待处理票据查询
        int busiType = Integer.valueOf(query.detail.busiType);
        String buscode;
        XmlTextWriter xmlWriter = new XmlTextWriter();
        try {
                header.FUNNAM = "SDKNTTRSECD";
                writeCommonHeader(xmlWriter, header);
                xmlWriter.writeStartElement("NTTRSDRFY");
//                BUSCOD 业务类型 C(6) B.4 业务类型 可 空查询全部
//                String busCode;
//                if("1".equals(query.detail.operType)){
//                    switch (Integer.valueOf(query.detail.busiType)){
//                        case 1:
//                        case 2:
//                        case 3:busCode = "N29012";break;
//                        case 4:busCode = "N29016";break;
//                        case 5:
//                        case 10:busCode = "N29013";break;
//                        case 12:
//                        case 13:
//                        case 14:busCode = "N29015";break;
//                        default:
//                            throw new Exception("未知的申请业务类型:"+query.detail.busiType);
//                    }
//                }else if("4".equals(query.detail.operType)){
//                    switch (Integer.valueOf(query.detail.busiType)){
//                        case 1:busCode = "N29012";break;
//                        default:busCode = "N29011";break;
//                    }
//                }else{
//                    busCode = "N29014";
//                }
                xmlWriter.writeElementString("BUSCOD","N29010",true);
//                BILTYP 票据种类 C(4) A.1 票据种类 可
                String billType;
                if("1".equals(query.detail.billType)){
                    billType = "AC01";
                }else  if("2".equals(query.detail.billType)) {
                    billType = "AC02";
                }else{
                    billType ="";
                }
                xmlWriter.writeElementString("BILTYP",billType,true);
//                BGNDAT 起始日期 D 否
                String begindate = query.detail.operateDate.substring(0,10).replace("-","");
                // TODO: 2019/4/26
               String begindateTest = getBankConfig().getValueAsString("queryDate");
            if(StringHelper.hasAnyChar(begindateTest)){
                begindate = begindateTest;
            }
                xmlWriter.writeElementString("BGNDAT",begindate,true);
//                ENDDAT 结束日期 D 否
                xmlWriter.writeElementString("ENDDAT",begindate,true);
//                OPRLGN 经办用户 Z(30) 可
                xmlWriter.writeElementString("OPRLGN","",true);
//                DATFLG 日期类型 C(1) A.8 日期类型 否
                xmlWriter.writeElementString("DATFLG","A",true);
//                CODFLG 提出/提回 C(1) A.9 提出/提回 标记 否 查询全部传’*’
                String codflg;
                if("1".equals(query.detail.operType)){
                    codflg = "O";
                }else if("4".equals(query.detail.operType)){
                    if("1".equals(query.detail.busiType)){
                        codflg = "O";
                    }else {
                        codflg = "C";
                    }
                }else{
                    codflg = "I";
                }
                xmlWriter.writeElementString("CODFLG",codflg,true);
//                CODTYP 票 据 业 务 代 码 C(2) 否 见下面备注
                String codeType;
                if("1".equals(query.detail.operType)){
                    switch (Integer.valueOf(query.detail.busiType)){
                    case 1:
                        codeType ="01";
                        break;//出票
                    case 2:
                        codeType = "02";
                        break;//承兑
                    case 3:
                        codeType = "03";
                        break;//收票
                    case 4:
                        codeType = "10";
                        break;//背书
                    case 5:
                        codeType = "18";
                        break;//质押
                    case 6:
                        codeType = "19";
                        break;//质押解除
                    case 10:
                        codeType = "11";
                        break;//贴现
                    case 12:
                        if ("2".equals(query.detail.overdueFlag)) {
                            codeType = "21";
                        } else {
                            codeType = "20";
                        }
                        break;//提示付款
                    case 13:
                        codeType = "22";
                        break;//追索
                    case 14:
                        codeType = "23";
                        break;//清偿
                    default:
                        throw new Exception("未知的业务类型：" + query.detail.busiType);
                }
                }else if("4".equals(query.detail.operType)){
                    switch (busiType) {
                        case 1:codeType = "04";break;
                        case 2:
                            codeType = "02";
                            break;//承兑撤回
                        case 3:
                            codeType = "03";
                            break;//提示收票撤回
                        case 4:
                            codeType = "10";
                            break;//背书撤回
                        case 5:
                            codeType = "18";
                            break;//质押撤回
                        case 6:
                            codeType = "19";
                            break;//解质押撤回
                        case 10:
                            codeType = "11";
                            break;//贴现撤回
                        case 12:
                            if ("2".equals(query.detail.overdueFlag)) {
                                codeType = "21";
                            } else {
                                codeType = "20";
                            }
                            break;//提示付款撤回
                        case 13:
                            codeType = "22";
                            break;//追索撤回
                        case 14:
                            codeType = "23";
                            break;//清偿撤回
                        default:
                            throw new Exception("未知的业务类型：" + query.detail.busiType);
                    }
                }else{
                    switch (busiType) {
                        case 2:
                            codeType = "02";
                            break;//承兑签收
                        case 3:
                            codeType = "03";
                            break;//提示收票签收
                        case 4:
                            codeType = "10";
                            break;//背书签收
                        case 5:
                            codeType = "18";
                            break;//质押签收
                        case 6:
                            codeType = "19";
                            break;//质押解除签收
                        case 11:
                            codeType = "12";
                            break;//贴现赎回签收
                        case 12:
                            if("1".equals(query.detail.overdueFlag)){
                                codeType = "20";
                            }else{
                                codeType = "21";
                            }
                            break;//提示付款签收
                        case 14:
                            codeType = "23";
                            break;//同意清偿签收
                        default:
                            throw new Exception("未知的业务类型：" + query.detail.busiType);
                    }
                }
                xmlWriter.writeElementString("CODTYP",codeType,true);
//                MINAMT 最小金额 M 可
                xmlWriter.writeElementString("MINAMT","",true);
//                MAXAMT 最大金额 M 可
                xmlWriter.writeElementString("MAXAMT","",true);
//                AUTSTR 请求状态 C(30) B.2 业 务 请 求 状态 可 空查询全部。
                xmlWriter.writeElementString("AUTSTR","",true);
//                RTNSTR 返回结果 C(30) B.3 业 务 处 理 结果 可 空查询全部。可以输 入多个状态进行组合 查询，比如输入“SQ”查询成功和部分成功 的记录。
                xmlWriter.writeElementString("RTNSTR","",true);
//                YURREF 业务参考号 C(30) 可
                xmlWriter.writeElementString("YURREF",query.detail.reqSeqID,true);
//                ACCNBR 帐号 C(35) 可
                xmlWriter.writeElementString("ACCNBR",query.detail.actNum,true);
//                BILNBR 票据号码 C(30) 可
                xmlWriter.writeElementString("BILNBR","",true);
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
//                retMap = queryBill("", tmpStrRet, tmpRet);
                if(tmpStrRet[0].length() == 0)
                {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null)
                    {
                        String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                        if("0".equals(bankRetCode)) {
                            if(tmpBkReader.read("/CMBSDKPGK/NTWAUECDZ")) {
                                detail.respCode = "0";
                                detail.reqSeqID = query.detail.reqSeqID;
                                String result =  tmpBkReader.readedXElement.getSingleText("REQSTS");
                                if("FIN".equals(result)) {
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("RTNFLG");
                                    //当返回的状态码不是S，说明交易失败，需要查询失败原因
                                    if(!"S".equals(detail.transState)){
                                        WriteBankLogLn2("交易失败，查询失败原因");
                                        String reqnbr = tmpBkReader.readedXElement.getSingleText("REQNBR");
                                        detail.respInfo = getErrMsg(reqnbr);
                                    }
                                }else{
                                    detail.transState = result;
                                }
                                detail.billNo = tmpBkReader.readedXElement.getSingleText("BILNBR");
                                if("2".equals(query.detail.operType)||"3".equals(query.detail.operType)||
                                        (("1".equals(query.detail.operType)&&"14".equals(query.detail.busiType)))){
                                    detail.billState = queryBillInfoBySign(query);
                                }
                                if(StringHelper.isNullOrEmpty(detail.billState)) {
                                    detail.billState = getBillState(detail.billNo, query.detail.actNum);
                                }
                                padBillState("S",detail,query);

                            }else{
                                detail.respCode = "-1";
                                detail.respInfo = "银行暂时没处理这比交易,请稍后在查!";
                            }
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = bankRetCode+" - "+tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        }
                    }
                    else
                    {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                }
                else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
        }catch (Exception ex){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        //endregion
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     *
     * @param reqSeqID 流程实例号
     * @return
     * @throws Exception
     */
    public String getErrMsg(String reqSeqID)throws Exception{
        try {
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "SDKNTTRSBCH";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header);
//            NTTRSDTLX
            xmlWriter.writeStartElement("NTTRSDTLX");
//            REQNBR 流程实例号 C(10) 可
            xmlWriter.writeElementString("REQNBR",reqSeqID);
//            CLTKEY 服务平台键值 C(11) 可
            xmlWriter.writeElementString("CLTKEY","");
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpSbReader = loadBankRespXmlReader(tmpStrRet[1],tmpSbMsg);
                if(tmpSbReader!=null){
                    String errCode = tmpSbReader.getFirstText("/CMBSDKPGK/NTTRSDTLZ/CLTSTS");
                    return  getErrMsgByCode(errCode,"未知原因");
                }else {
                    throw new Exception(tmpSbMsg.toString());
                }
            } else {
                throw new Exception( tmpStrRet[1]);
            }
        }catch (Exception ex){
            ex.getStackTrace();
            throw new Exception("查询失败原因失败");
        }
    }

    /**
     * 根据错误码获取错误信息
     * @param errCode 错误码
     * @param defaultMsg 默认错误信息
     * @return
     */
    public String getErrMsgByCode(String errCode,String defaultMsg){
        Map<String,String> msgMap = new HashMap<String,String>();
        msgMap.put("50","清算失败");
        msgMap.put("A1","待签收");
        msgMap.put("D2","已驳回");
        msgMap.put("D1","已签收");
        msgMap.put("2","申请经办中");
        msgMap.put("1","取消");
        msgMap.put("0","初始");
        msgMap.put("30","撤销经办中");
        msgMap.put("B2","驳回经办中");
        msgMap.put("B1","签收经办中");
        msgMap.put("E1","清算失败");
        msgMap.put("13","撤票成功");
        msgMap.put("12","出票已登记");
        msgMap.put("11","回执失败");
        msgMap.put("41","已撤销");
        msgMap.put("10","申请发送待回执");
        msgMap.put("40","撤销发送待回执");
        msgMap.put("C2","驳回发送待回执");
        msgMap.put("C1","签收发送待回执");
        msgMap.put("F1","对方已撤销");
        msgMap.put("22","对方已签收");
        msgMap.put("21","对方已驳回");
        msgMap.put("20","对方待签收");
        String errMsg  =msgMap.get(errCode);
        if(StringHelper.isNullOrEmpty(errMsg)){
            return defaultMsg;
        }
        return errMsg;
    }

    //待签收者查询票据状态
    private String queryBillInfoBySign(ReqB006BillStateQry query) {
        try {
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "SDKNTWATAEX";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header);
//            NTINQDRFY
            xmlWriter.writeStartElement("NTWATANSY");
//           ACCNBR 帐号 C(35) 否
            xmlWriter.writeElementString("ACCNBR",query.detail.actNum);
//            BUSCOD 业务编码 C(50) 企业：A.10 参数
            String buscode;
//            03=提示收票回复
//            22=追索回复
//            23=同意清偿回复
//            19=质押解除回复
//            12=贴现赎回回复
//            10=背书转让回复
            if("2".equals(query.detail.busiType)){
                buscode= "02";
            }else if("3".equals(query.detail.busiType)){
                buscode= "03";
            }else if("4".equals(query.detail.busiType)){
                buscode= "10";
            }else if("5".equals(query.detail.busiType)){
                buscode= "18";
            }else if("6".equals(query.detail.busiType)){
                buscode= "19";
            }else if("11".equals(query.detail.busiType)){
                buscode= "12";
            }else if("12".equals(query.detail.busiType)){
                if("1".equals(query.detail.overdueFlag)){
                    buscode= "20";
                }else {
                    buscode= "21";
                }
            }else if("14".equals(query.detail.busiType)){
                if("1".equals(query.detail.operType)) {
                    buscode = "22";
                }else{
                    buscode = "23";
                }
            }else{
                throw new Exception("未知的操作类型！"+query.detail.busiType);
            }
            xmlWriter.writeElementString("BUSCOD",buscode);
//            BILNBR 人 行 电 子 票 据号码 C(30) 可
            xmlWriter.writeElementString("BILNBR",query.detail.billNo);
//            CLTSTS 服务状态 C(2) A1：提回待处理 20：可撤销 否
            xmlWriter.writeElementString("CLTSTS","A1");
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpSbReader = loadBankRespXmlReader(tmpStrRet[1],tmpSbMsg);
                if(tmpSbReader!=null){
                    return tmpSbReader.getFirstText("/CMBSDKPGK/NTDRFDTLZ1/PBLSTS");
                }else {
                    throw new Exception(tmpSbMsg.toString());
                }
            } else {
                throw new Exception( tmpStrRet[1]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "";
    }

    //    获取票据状态
    public String getBillState(String billNo,String rightAcc){
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "SDKNTECDBCH";
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeStartElement("NTDRFDTLY");
//            NTDRFDTLY BILNBR 人行电子票据号码 C(30) 否
            xmlTextWriter.writeElementString("BILNBR",billNo);
//            OWNEAC 权利人帐号 C(35) 否
            xmlTextWriter.writeElementString("OWNEAC",rightAcc);
            xmlTextWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlTextWriter.toXmlString());
            if(tmpStrRet[0].length() == 0)
            {
                StringBuilder getBankRetInfo = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                if (tmpBkReader != null)
                    {
                       return tmpBkReader.getFirstText("/CMBSDKPGK/NTDRFDTLZ1/PBLSTS");
                    }
                else
                {
                    throw new Exception(tmpBkReader.toString());
                }
            }
            else{
                throw new Exception(tmpStrRet[0].toString());
            }

        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "";
    }

    @Override
    /**
     * 可追索人查询 不支持
     */
    public RetAtsB007BillKzsrQry D_B007_BillKzsr_Query(ReqB007BillKzsrQry query) throws Exception {
        RetAtsB007BillKzsrQry retAts = new RetAtsB007BillKzsrQry(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        retAts.respCode = "0";
        retAts.billNo = query.detail.billNo;
        retAts.writeRespInfos();
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "SDKNTECDBCH";
            writeCommonHeader(xmlTextWriter,header);
//            NTDRFDTLY BILNBR 人行电子票据号码 C(30) 否
            xmlTextWriter.writeStartElement("NTDRFDTLY");
            xmlTextWriter.writeElementString("BILNBR",query.detail.billNo);
//            OWNEAC 权利人帐号 C(35) 否
            xmlTextWriter.writeElementString("OWNEAC",query.detail.actNum);
            xmlTextWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlTextWriter.toXmlString());
            if(tmpStrRet[0].length() == 0)
            {
                StringBuilder getBankRetInfo = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                if (tmpBkReader != null)
                {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
                        if(tmpBkReader.read("/CMBSDKPGK/NTDRFDTLZ1")){
                            RetAtsB007BillKzsrQryDetail detailDraw = new RetAtsB007BillKzsrQryDetail();
//                            DRWORG 出票人组织机 构代码 C(20) 可
                              detailDraw.receCode = tmpBkReader.readedXElement.getSingleText("DRWORG");
//                            DRWNAM 出票人名称 Z(200) 否
                            detailDraw.receName = tmpBkReader.readedXElement.getSingleText("DRWNAM");
//                            DRWBRD 出票人行号 C(12) 可
                            detailDraw.receBnkCode = tmpBkReader.readedXElement.getSingleText("DRWBRD");
//                            DRWBAM 出票人开户银 行 Z(200) 可
                            detailDraw.receBnkName = tmpBkReader.readedXElement.getSingleText("DRWBAM");
//                            DRWEAC 出票人账号
                            detailDraw.receAct = tmpBkReader.readedXElement.getSingleText("DRWEAC");
                            retAts.writeDetail(detailDraw);
                            RetAtsB007BillKzsrQryDetail detailAccp = new RetAtsB007BillKzsrQryDetail();
//                            ACPORG 承兑人组织机 构代码 C(20) 可
                            detailAccp.receCode = tmpBkReader.readedXElement.getSingleText("ACPORG");
//                            ACPNAM 承兑人名称 Z(200) 可
                            detailAccp.receName = tmpBkReader.readedXElement.getSingleText("ACPNAM");
//                            ACPBRD 承兑人行号 C(12) 可
                            detailAccp.receBnkCode = tmpBkReader.readedXElement.getSingleText("ACPBRD");
//                            ACPBAM 承兑人开户银 行 Z(200) 可
                            detailAccp.receBnkName = tmpBkReader.readedXElement.getSingleText("ACPBAM");
//                            ACPEAC 承兑人账号 C(35)
                            detailAccp.receAct = tmpBkReader.readedXElement.getSingleText("ACPEAC");
                            retAts.writeDetail(detailAccp);
                        }
                        while (tmpBkReader.read("/CMBSDKPGK/NTDRFDTLZ2")){
                            String buscode = tmpBkReader.readedXElement.getSingleText("BUSCOD");
                            //背书
                            if("10".equals(buscode)){
                                RetAtsB007BillKzsrQryDetail detail = new RetAtsB007BillKzsrQryDetail();
//                                ISCORG 发起方组织机 构代码 C(20) 可
                                detail.receCode = tmpBkReader.readedXElement.getSingleText("receCode");
//                                ISCNAM 发起方名称 Z(350) 可
                                detail.receName = tmpBkReader.readedXElement.getSingleText("ISCNAM");
//                                ISCBRD 发起方行号 C(12) 可
                                detail.receBnkCode = tmpBkReader.readedXElement.getSingleText("ISCBRD");
//                                ISCEAC 发起方账号 C(35) 可
                                detail.receAct = tmpBkReader.readedXElement.getSingleText("ISCEAC");
                                retAts.writeDetail(detail);
                            }
                        }
                    }else{
                        String errinfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG ");
                        throw new Exception(errinfo);
                    }
                }
                else
                {
                    throw new Exception(getBankRetInfo.toString());
                }
            }
            else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts = new RetAtsB007BillKzsrQry(query.reqHeader);
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
            retAts.billNo = query.detail.billNo;
            retAts.writeBeforeDetail();
            retAts.writeRespInfos();

        }
        retAts.writeAfter();
        return retAts;
    }

    @Override
    /**
     * 历史票据查询 不支持
     */
    public RetAtsB008HistoricalBillQry D_B008_HistoricalBillQuery(ReqB008HistoricalBillQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB009DrawBillQry D_B009_DrawBillQuery(ReqB009DrawBillQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB010ProtocolStateQry D_B010_ProtocolStateQuery(ReqB010ProtocolStateQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB011TransFlagQry D_B011_TransFlagQuery(ReqB011TransFlagQry query) throws Exception {
        return null;
    }

    /**
    * 查询票据状态、可操作票据、返回平台处理号/票据状态
    * */
    public Map<String,String> queryBill(String overPayFlag, String[] tmpStrRet, RetAtsB001OperBillQry retAts,String queryBillType) throws Exception{
        Map<String,String> retMap = new HashMap<String, String>(16);
        String retId  = "";
        RetAtsB001OperBillQryDetail detail;
        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String retCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                String retMessage  = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                if(!retAts.hasWrittenRespCode) {
                    retAts.writeRespCode(retCode);
                    retAts.writeRespInfo(retMessage);
                }
                detail  = new RetAtsB001OperBillQryDetail();
                String detailPath =  "/CMBSDKPGK/NTWATANSZ";
                while(tmpBkReader.read(detailPath)){
                    String billtype = "AC01".equals(tmpBkReader.readedXElement.getSingleText("BILTYP"))?"1":"2";
                    if(queryBillType.equals(billtype)) {
                        detail.billNo = tmpBkReader.readedXElement.getSingleText("BILNBR");
                        detail.busiNo = tmpBkReader.readedXElement.getSingleText("CLTKEY");
                        detail.applyCode = tmpBkReader.readedXElement.getSingleText("CLTBRN");
                        detail.applyName = tmpBkReader.readedXElement.getSingleText("OPCNAM");
                        detail.applyAct = tmpBkReader.readedXElement.getSingleText("OPCEAC");
                        if ("1".equals(billtype)) {
                            detail.billType = "EM001";
                        } else if ("2".equals(billtype)) {
                            detail.billType = "EM002";
                        }
                        String applyDate = tmpBkReader.readedXElement.getSingleText("APPDAT");
                        if (StringHelper.hasAnyChar(applyDate)) {
                            detail.applyDate = DateHelper.convertD8toD10(applyDate);
                        }
                        String issDate = tmpBkReader.readedXElement.getSingleText("ISUDAT");
                        if (StringHelper.hasAnyChar(issDate)) {
                            detail.issDate = DateHelper.convertD8toD10(issDate);
                        }
                        String dueDate = tmpBkReader.readedXElement.getSingleText("DUEDAT");
                        if (StringHelper.hasAnyChar(dueDate)) {
                            detail.dueDate = DateHelper.convertD8toD10(dueDate);
                        }
                        detail.billAmt = tmpBkReader.readedXElement.getSingleText("TRXAMT");
                        String transFlag = tmpBkReader.readedXElement.getSingleText("NOTTRF");
                        if ("EM01".equals(transFlag)) {
                            detail.transFlag = "0";
                        } else if ("EM00".equals(transFlag)) {
                            detail.transFlag = "1";
                        }
                        detail.overDueFlag = overPayFlag;
                        detail.billStatus = getBillState(detail.billNo, detail.applyAct);
                        retAts.writeDetail(detail);
//                    if(billNo.equals(detail.billNo)){
//                        //服务平台键值
//                        retId = tmpBkReader.readedXElement.getSingleText("CLTKEY");
//                        retMap.put(retId, detail.billStatus);
//                    }
                    }
                }
            }
            else
            {
//                retAts.writeRespCode("-1");
//                retAts.writeRespInfo(tmpBkReader.toString());
                throw new Exception(tmpBkReader.toString());
            }
        }
        else{
//            retAts.writeRespCode("-1");
//            retAts.writeRespInfo(tmpStrRet[0].toString());
            throw new Exception(tmpStrRet[0].toString());
        }
        return retMap;
    }

    public List<RetAtsB005BillBackQryDetail> queryBillInfo(String[] tmpStrRet) throws Exception{
        RetAtsB005BillBackQryDetail backInfo =  new RetAtsB005BillBackQryDetail();
        ArrayList<RetAtsB005BillBackQryDetail>  backInfos = new ArrayList<RetAtsB005BillBackQryDetail> ();
//        String frontPath  = "/CMBSDKPGK/NTDRFDTLZ1";
        String backPath =  "/CMBSDKPGK/NTDRFDTLZ2";
        if(tmpStrRet[0].length() == 0) {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null) {
                String retCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                String retMsg = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                if ("0".equals(retCode)) {
                    while (tmpBkReader.read(backPath)) {
                        backInfo = new RetAtsB005BillBackQryDetail();
                        String busCode = tmpBkReader.readedXElement.getSingleText("BUSCOD");
                        //10  背书转让 11  贴现 12  回购式贴现赎回 13  转贴现 14  回购式转贴现赎回 15  再贴现 16  回购式再贴现赎回 25  央行卖出商业汇票 17  保证 18  质押 19  质押解除 20  提示付款 21  逾期提示付款 22  追索 23  追索同意清偿
                        switch (Integer.valueOf(busCode)) {
                            // 19=质押解除
                            case 19:
                                busCode = "19";
                                break;
//                    18=质押
                            case 18:
                                busCode = "18";
                                break;
//                    16=再贴赎回
                            case 16:
                                busCode = "16";
                                break;
//                    15=再贴
                            case 15:
                                busCode = "15";
                                break;
//                    14=转贴赎回
                            case 14:
                                busCode = "14";
                                break;
//                    13=转贴
                            case 13:
                                busCode = "13";
                                break;
//                    12=贴现赎回
                            case 12:
                                busCode = "12";
                                break;
//                    11=贴现
                            case 11:
                                busCode = "11";
                                break;
//                    10=背书转让
                            case 10:
                                busCode = "10";
                                break;
//                    23=清偿
                            case 23:
                                busCode = "23";
                                break;
//                    22=追索
                            case 22:
                                busCode = " 22";
                                break;
//                    21=逾期提示付
                            case 21:
                                busCode = "21";
                                break;
//                        20=提示付款
                            case 20:
                                busCode = "20";
                                break;
                            default:
                                continue;
                        }
                        backInfo.respCode = retCode;
                        backInfo.transCode = busCode;
                        backInfo.billNo = tmpBkReader.readedXElement.getSingleText("BILNBR");
                        backInfo.applyName = tmpBkReader.readedXElement.getSingleText("ISCNAM");
                        backInfo.receName = tmpBkReader.readedXElement.getSingleText("OPCNAM");
                        backInfo.repDueDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("ENDDAT"));
                        backInfo.repOpenDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("OPNDAT"));
                        String tranFlag = tmpBkReader.readedXElement.getSingleText("NOTTRF");
                        if(StringHelper.hasAnyChar(tranFlag)&&"EM00".equals(tranFlag)){
                            backInfo.transFlag = "1";
                        }else if(StringHelper.hasAnyChar(tranFlag)&&"EM01".equals(tranFlag)){
                            backInfo.transFlag = "0";
                        }
                        String ansCode = tmpBkReader.readedXElement.getSingleText("ANSCOD");
                        if ("SU00".equals(ansCode)) {
                            backInfo.respFlag = "1";
                        } else if ("SU01".equals(ansCode)) {
                            backInfo.respFlag = "2";
                        }
                        String sgnDat = tmpBkReader.readedXElement.getSingleText("SGNDAT");
                        if (StringHelper.hasAnyChar(sgnDat)) {
                            backInfo.respDate = DateHelper.convertD8toD10(sgnDat);
                        }
                        String RFSCOD = tmpBkReader.readedXElement.getSingleText("RFSCOD");
                        if (StringHelper.hasAnyChar(RFSCOD)) {
                            if ("DC99".equals(retCode)) {
                                backInfo.unPayCode = "9";
                            } else {
                                backInfo.unPayCode = Integer.valueOf(retCode.substring(3)) - 1 + "";
                            }
                        }

                        String TROTYP = tmpBkReader.readedXElement.getSingleText("TROTYP");
                        if (StringHelper.hasAnyChar(TROTYP)) {
                            if ("RT01".equals(TROTYP)) {
                                backInfo.recourseType = "2";
                            } else {
                                backInfo.recourseType = "1";
                            }
                        }
                        backInfos.add(backInfo);
                    }
                } else {
                    throw new Exception(retMsg);
                }
            } else {
                throw new Exception(getBankRetInfo.toString());
            }
        }else{
            throw new Exception(tmpStrRet[1]);
        }

        return backInfos;
    }
}
